package com.boilerCloud.controller.device;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.device.ModelType;
import com.boilerCloud.dto.sys.ReqResult;
import com.boilerCloud.service.device.IModelService;


@RestController
@RequestMapping("/model/")
public class ModelTypeControler {
	private static final Logger log = LoggerFactory.getLogger(ModelTypeControler.class);
	@Resource
	private IModelService modelService;


	@RequestMapping(value = "modelTypeList")
	public ModelAndView modelTypeList() {
		return new ModelAndView("/views/model/modelType.html");
	}
	
	

	/**
	 * 查询模组类型列表
	 * @param grade
	 * @param yema
	 * @param length
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "qryModelTypeList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult qryModelTypeList(@ModelAttribute ModelType model ,
			@RequestParam(required = true) int yema,
			@RequestParam(required = true) int length,HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			
			HzlqswPage<ModelType> page = new HzlqswPage<ModelType>();
			page.setCurPage(yema);
			page.setPageSize(length);
			modelService.qryModelTypeList(model,page);
			rst.setReturnObject(page);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("查询模组类型列表失败");
			log.error("查询模组类型列表异常",e);
			return rst;
		}
	}
	
	/**
	 * 修改模组信息
	 * @param device
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "editModelType", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult editModel(
			@ModelAttribute  ModelType model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			modelService.editModelType(model);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("修改模组信息失败");
			log.error("修改模组信息异常",e);
			return rst;
		}
	}
	/**
	 * 新增模组
	 * @param device
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addModelType", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult addModelType(
			@ModelAttribute  ModelType model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			modelService.addModelType(model);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("新增模组信息失败");
			log.error("新增模组信息异常",e);
			return rst;
		}
	}
	
	/**
	 * 删除模组类型
	 * @param businessNo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "delModelType", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult delModel(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			modelService.delModelType(id);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("删除模组类型失败");
			log.error("删除模组类型异常",e);
			return rst;
		}
	}
	

	/**
	 * 获取模组类型列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getModelTypeList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getModelTypeList(
			@ModelAttribute  ModelType model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			List<ModelType> list = modelService.getModelTypeList(model);
			rst.setResultCode(ReqResult.resultCode_Success);
			rst.setReturnObject(list);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("获取模组类型列表失败");
			log.error("获取模组类型列表异常",e);
			return rst;
		}
	}
	
	@RequestMapping(value = "getModelType", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getModelType(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			ModelType model = modelService.getModelType(id);
			rst.setResultCode(ReqResult.resultCode_Success);
			rst.setReturnObject(model);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("获取模组类型失败");
			log.error("获取模组类型异常",e);
			return rst;
		}
	}
	
	

}
